// TypeScript file
class GamePlayView extends egret.Sprite
{
    private container: egret.Bitmap;
    private ready: StarlingSwfMovieClip;
    private target: GameFightView;
    private optionView: OptionView;
    private sp: egret.Sprite;
    constructor()
    {
        super();
        this.sp = new egret.Sprite();
        this.sp.touchEnabled = true;
        this.addChild(this.sp);
    }

    public showGame(curScene: number): void
    {
        GameData.isStartClickOption = true;
        GameData.isStart = false;

        let optionBtn: egret.Sprite = new egret.Sprite();
        let optionBmp: egret.Bitmap = ResourceUtils.createBitmapByName("option_btn_b_png");
        optionBtn.addChild(optionBmp);
        optionBtn.touchEnabled = true;
        optionBtn.x = Const.SENT_WIDTH - optionBtn.width;
        optionBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showOptionView, this);

        this.sp.removeChildren();
        switch (curScene)
        {
            case 1:
            {
                let game: GameFightOneView = new GameFightOneView();
                this.sp.addChild(game);
                this.target = game;
                this.container = ResourceUtils.createBitmapByName("gameinfo_1_png");
                this.sp.addChild(this.container);
            }
            break;
            case 2:
            {
               let game: GameFightTwoView = new GameFightTwoView();
               this.sp.addChild(game);
               this.target = game;
               GameData.isPause = true;
               this.container = ResourceUtils.createBitmapByName("gameinfo_2_png");
               this.sp.addChild(this.container);

            }
            break;
        }

        this.addChild(optionBtn);

        this.optionView = new OptionView();
        this.addChild(this.optionView);
        this.optionView.visible = false;
        this.sp.addEventListener(egret.TouchEvent.TOUCH_TAP, this.startHandler, this);

    }

    private startHandler(evt: egret.TouchEvent): void
    {
        if (this.optionView.visible) return;

        if (GameData.isClickBtn) return;

        GameData.isStart = true;
        this.sp.removeChild(this.container);
        this.sp.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.startHandler, this);
        this.container = null;
        this.sp.touchEnabled = false;
        this.ready = StarlingSwfFactory.getInstance().makeMc("go");
        this.ready.x = Const.SENT_WIDTH / 2;
        this.ready.y = Const.SENT_HEIGHT / 2;
        this.addChild(this.ready);
        this.ready.gotoAndPlay("1");
        SoundUtils.instance().play(SoundType.NUMSOUND);
        GameData.isStartClickOption = false;
        this.ready.setCompleteAction(this.complete1, this);
    }

    private showOptionView(): void
    {

    }

    private complete1(): void
    {
        this.ready.gotoAndPlay("2");
        SoundUtils.instance().play(SoundType.GOSOUND);
        egret.setTimeout(this.complete2.bind(this), this, 300);
    }

    private complete2(): void
    {
        GameData.isStartClickOption = true;
        GameData.isStart = true;
        GameData.isPause = false;
        SoundUtils.instance().play(SoundType.BGSOUND);
        this.target.redGirl.run();
        this.removeChild(this.ready);
    }
}